package com.ionehe.group.common.file.ex.model;

import com.ionehe.group.common.file.ex.repository.dataobject.UserFile;
import com.ionehe.group.common.file.ex.repository.dataobject.UserFolder;
import lombok.Data;

import java.io.Serializable;
import java.util.Objects;

/**
 * 文件上传返回对象
 * @author 2022/1/14 <a href="mailto:a@ionehe.com">xiu</a>
 */
@Data
public class UploadDTO implements Serializable {

    private static final long serialVersionUID = -38331060124340967L;

    /**
     * 类型(文件夹|文件)
     */
    private Integer type;

    /**
     * 文件信息
     */
    private UserFile userFile;

    /**
     * 文件夹信息
     */
    private UserFolder userFolder;

    private String error;


    public UploadDTO(UserFile userFile) {
        this.type = Type.FILE.toNumber();
        this.userFile = userFile;
    }

    public UploadDTO(UserFolder userFolder) {
        this.type = Type.FOLDER.toNumber();
        this.userFolder = userFolder;
    }

    public UploadDTO(UserFile userFile, String error) {
        this.type = Type.FILE.toNumber();
        this.userFile = userFile;
        this.error = error;
    }

    public enum Type {
        /**
         * 文件类型
         */

        FOLDER(1, "文件夹"),
        FILE(2, "文件");

        private final Integer value;

        private final String display;

        private Type(Integer value, String display) {
            this.value = value;
            this.display = display;
        }

        public static Type fromNumber(Integer value) {
            for (Type t : Type.values()) {
                if (Objects.equals(t.value, value)) {
                    return t;
                }
            }
            return null;
        }

        public Integer toNumber() {
            return this.value;
        }

        @Override
        public String toString() {
            return this.display;
        }
    }
}
